package cqrtplm.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lark.oapi.okhttp.OkHttpClient;
import com.lark.oapi.okhttp.Request;
import com.lark.oapi.okhttp.Response;
import cqrtplm.dto.ClientCodeDTO;
import cqrtplm.service.RTCrmIntegrityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class RTCrmIntegrityServiceImpl implements RTCrmIntegrityService {

    @Value("${crm.client.code.url}")
    private String getClientCodeListUrl;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public List<ClientCodeDTO> getClientCodeList(String clientName) {
        String catchKey = "getClientCodeListFromCrm";
        List<ClientCodeDTO> catchData = (List<ClientCodeDTO>)redisTemplate.opsForValue().get(catchKey);
        if (CollUtil.isNotEmpty(catchData)) {
            return bulidClientCodeList(clientName,catchData);
        }
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        Request request = new Request.Builder()
                .url(getClientCodeListUrl+"?clientcode&sapclientcode")
                .method("GET", null)
                .build();
        List<ClientCodeDTO> list = new ArrayList<>();
        try {
            Response response = client.newCall(request).execute();
            if (response.body() != null) {
                String info = response.body().string();
                JSONObject infoJSONObject = JSONObject.parseObject(info);
                JSONArray infoList = infoJSONObject.getJSONArray("data");
                for (int i = 0; i < infoList.size(); i++) {
                    JSONObject infoObject = infoList.getJSONObject(i);
                    String clientName2 = infoObject.getString("clientcode");
                    String clientCode2 = infoObject.getString("sapclientcode");
                    String text = infoObject.getString("text");
                    ClientCodeDTO dto = new ClientCodeDTO();
                    dto.setClientCode(clientCode2);
                    dto.setClientName(clientName2);
                    dto.setText(text);
                    list.add(dto);
                }
                redisTemplate.opsForValue().set(catchKey, list, 30 , TimeUnit.MINUTES);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return bulidClientCodeList(clientName,list);
    }

    private List<ClientCodeDTO> bulidClientCodeList(String clientName,List<ClientCodeDTO> list) {
        if(StringUtils.isEmpty(clientName) && StringUtils.isBlank(clientName)){
            return list;
        }
        List<ClientCodeDTO> ClientCodeDTOS = new ArrayList<>();
        for (ClientCodeDTO clientCodeDTO : list) {
            if (clientCodeDTO.getClientName().contains(clientName) || clientCodeDTO.getClientCode().contains(clientName)) {
                ClientCodeDTOS.add(clientCodeDTO);
            }
        }
        return ClientCodeDTOS;
    }
}
